package com.yt.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.project.mapper.KuaishouMapper;
import com.yt.project.model.dto.douyin.DouyinQueryRequest;
import com.yt.project.model.dto.kuaishou.KuaishouQueryRequest;
import com.yt.project.model.entity.Bilibili;
import com.yt.project.model.entity.Douyin;
import com.yt.project.model.entity.Kuaishou;
import com.yt.project.model.vo.DescVO;
import com.yt.project.model.vo.DouyinVO;
import com.yt.project.model.vo.KuaishouVO;
import com.yt.project.model.vo.TagDistVO;
import com.yt.project.service.KuaishouService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【kuaishou】的数据库操作Service实现
* @createDate 2023-04-19 21:46:47
*/
@Service
public class KuaishouServiceImpl extends ServiceImpl<KuaishouMapper, Kuaishou>
    implements KuaishouService{

    @Override
    public Page<KuaishouVO> pageKuaishou(KuaishouQueryRequest kuaishouQueryRequest) {
        long current = 1;
        long pageSize = 10;
        if (kuaishouQueryRequest != null) {
            current = kuaishouQueryRequest.getCurrent();
            pageSize = kuaishouQueryRequest.getPageSize();
        }
        if (kuaishouQueryRequest == null) {
            kuaishouQueryRequest = new KuaishouQueryRequest();
        }
        QueryWrapper<Kuaishou> kuaishouQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(kuaishouQueryRequest.getSearchText())) {
            kuaishouQueryWrapper.like("nickname", kuaishouQueryRequest.getSearchText());
        }

        if (kuaishouQueryRequest.getShare() != null) {
            kuaishouQueryWrapper.gt("share", kuaishouQueryRequest.getShare());
        }
        if (kuaishouQueryRequest.getComment() != null) {
            kuaishouQueryWrapper.gt("comment", kuaishouQueryRequest.getComment());
        }
        if (kuaishouQueryRequest.getZan() != null) {
            kuaishouQueryWrapper.gt("zan", kuaishouQueryRequest.getZan());
        }
        if (kuaishouQueryRequest.getFan() != null) {
            kuaishouQueryWrapper.gt("fan", kuaishouQueryRequest.getFan());
        }
        if (kuaishouQueryRequest.getTypes() != null && kuaishouQueryRequest.getTypes().size() > 0) {
            kuaishouQueryWrapper.in("ttype", kuaishouQueryRequest.getTypes());
        }

        if (kuaishouQueryRequest.getStartDate() != null && kuaishouQueryRequest.getEndDate() != null) {
            // 数据库startTime > bilibiliQueryRequest.getStartDate()  && 数据库endDate < bilbxxxx
            kuaishouQueryWrapper.gt("startTime", kuaishouQueryRequest.getStartDate());
            kuaishouQueryWrapper.lt("endTime", kuaishouQueryRequest.getEndDate());
        }

        Page<Kuaishou> kuaishouPage = this.page(new Page<>(current, pageSize), kuaishouQueryWrapper);
        List<Kuaishou> kuaishous = kuaishouPage.getRecords();
        List<KuaishouVO> bilibiliVOS = kuaishous.stream().map(kuaishou -> {
            KuaishouVO kuaishouVO = new KuaishouVO();
            kuaishouVO.setId(kuaishou.getId());
            kuaishouVO.setNickname(kuaishou.getNickname());
            kuaishouVO.setNewPost(kuaishou.getNewPost());
            kuaishouVO.setShare(kuaishou.getShare());
            kuaishouVO.setComment(kuaishou.getComment());
            kuaishouVO.setZan(kuaishou.getZan());
            kuaishouVO.setFan(kuaishou.getFan());
            String type = kuaishou.getTtype();
            List<String> types = new ArrayList<>();
            types.add(type);
            kuaishouVO.setTypes(types);
            kuaishouVO.setAvatar(kuaishou.getAvatarUrl());
            return kuaishouVO;
        }).collect(Collectors.toList());
        Page<KuaishouVO> kuaishouVOPage = new Page<>();
        kuaishouVOPage.setRecords(bilibiliVOS);
        kuaishouVOPage.setTotal(kuaishouPage.getTotal());
        return kuaishouVOPage;
    }

    @Override
    public DescVO getDescById(Integer id) {
        QueryWrapper<Kuaishou> queryWrapper = new QueryWrapper<>();
        Kuaishou kuaishou = this.getById(id);
        String nickname = kuaishou.getNickname();
        QueryWrapper<Kuaishou> kuaishouQueryWrapper = new QueryWrapper<>();
        kuaishouQueryWrapper.eq("nickname", nickname);
        kuaishouQueryWrapper.orderByAsc("start_date");
        List<Kuaishou> kuaishouList = this.list(kuaishouQueryWrapper);
        // 计算权重：
        List<String> dates = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        kuaishouList.forEach(b -> {
            // 0.2
            Long share = b.getShare();
            // 0.2
            Long comment = b.getComment();
            Long fan = b.getFan();
            Long newFan = b.getNewFan();
            Long zan = b.getZan();
            // 计算权重
            Double data = 0.2 * share + 0.2 * comment + 0.2 * fan + 0.2 * newFan + 0.2 * zan;
            Date startDate = b.getStartDate();
            Date endDate = b.getEndDate();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String date = formatter.format(startDate) + "-\n" + formatter.format(endDate);
            dates.add(date);
            datas.add(data.intValue() / 100);
        });
        DescVO descVO = new DescVO();
        descVO.setFan(String.valueOf(kuaishou.getFan() > 10000 ? (kuaishou.getFan() / 10000) + "w" : kuaishou.getFan()));
        descVO.setComment(String.valueOf(kuaishou.getComment() > 10000 ? (kuaishou.getComment() / 10000) + "w" : kuaishou.getComment()));
        descVO.setShare(String.valueOf(kuaishou.getShare() > 10000 ? (kuaishou.getShare() / 10000) + "w" : kuaishou.getShare()));
        descVO.setId(kuaishou.getId());
        descVO.setNickname(kuaishou.getNickname());
        descVO.setAvatarUrl(kuaishou.getAvatarUrl());
        descVO.setDate(dates);
        descVO.setData(datas);
        return descVO;
    }

    @Override
    public List<TagDistVO> getTagsDist(Integer id) {
        QueryWrapper<Kuaishou> kuaishouQueryWrapper = new QueryWrapper<>();
        kuaishouQueryWrapper.select("ttype");
        List<Kuaishou> kuaishouList = this.list(kuaishouQueryWrapper);
        List<String> tagList = kuaishouList.stream().map(Kuaishou::getTtype).collect(Collectors.toList());
        //System.out.println(tagList);
        Map<String, Long> map = new HashMap<>();
        for (String tag : tagList) {
            if (map.containsKey(tag)) {
                map.put(tag, map.get(tag) + 1);
            } else {
                map.put(tag, 1L);
            }
        }
        List<TagDistVO> tagDistVOList = new ArrayList<>();
        map.forEach((k, v) -> {
            TagDistVO tagDistVO = new TagDistVO();
            tagDistVO.setName(k);
            tagDistVO.setValue(v);
            tagDistVOList.add(tagDistVO);
        });
        return tagDistVOList;
    }
}




